//
//  OfficeViewController.m
//  e律师
//
//  Created by 韦淋 on 2017/5/15.
//  Copyright © 2017年 test. All rights reserved.
//

#import "OfficeViewController.h"


static NSString *reuseID = @"itemCell";
static NSString *sectionHeaderID = @"sectionHeader";
@interface OfficeViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>
{
    NSIndexPath *_originalIndexPath;
    NSIndexPath *_moveIndexPath;
    UIView *_snapshotView;
}
@property (nonatomic, strong) UIView *barView;
@property (nonatomic, strong) UILabel *barTitleLable;
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSArray *itemGroups;
@property (nonatomic, strong) NSArray *allItemModel;
@property (nonatomic, assign) BOOL isEditing;

@end

@implementation OfficeViewController
- (void)setIsEditing:(BOOL)isEditing {
    _isEditing = isEditing;
    UIBarButtonItem *managerItem = self.navigationItem.rightBarButtonItem;
    UIButton *managerButton = managerItem.customView;
    managerButton.selected = isEditing;
}
- (void)viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBar.hidden = YES;
    self.tabBarController.tabBar.hidden = NO;
    //    self.automaticallyAdjustsScrollViewInsets = false;
}
//- (void)viewWillDisappear:(BOOL)animated{
//    self.tabBarController.tabBar.hidden = YES;
//}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.view.backgroundColor = [UIColor colorWithRed:238.0f/255.0f green:238.0f/255.0f blue:238.0f/255.0f alpha:1.0f];
    [self.view addSubview:self.barView];
    [self.barView addSubview:self.barTitleLable];
    [self setupCollectionView];
}


- (void)setupCollectionView {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(SCREEN_WIDTH / 4, SCREEN_WIDTH / 4);
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
    layout.headerReferenceSize = CGSizeMake(SCREEN_WIDTH, 35);
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64, self.view.bounds.size.width, self.view.bounds.size.height - 110) collectionViewLayout:layout];
    _collectionView.backgroundColor = [UIColor whiteColor];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    [self.view addSubview:_collectionView];
    [_collectionView registerClass:[ItemCell class] forCellWithReuseIdentifier:reuseID];
    [_collectionView registerClass:[ItemCellHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:sectionHeaderID];
    //UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
    //[_collectionView addGestureRecognizer:longPress];
    
}


#pragma mark - <UICollectionViewDelegate, UICollectionViewDataSource>
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return self.itemGroups.count;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    ItemGroup *group = self.itemGroups[section];
    return group.items.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    ItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseID forIndexPath:indexPath];
//    cell.delegate = self;
//    cell.backgroundColor = kRandomColor;
    ItemGroup *group = self.itemGroups[indexPath.section];
    ItemModel *itemModel = group.items[indexPath.row];
    if (indexPath.section != 0) {
        BOOL isAdded = NO;
        ItemGroup *homeGroup = self.itemGroups[0];
        for (ItemModel *homeItemModel in homeGroup.items) {
            
            if ([homeItemModel.itemTitle isEqualToString:itemModel.itemTitle]) {
                isAdded = YES;
                break;
            }
        }
        
        if (isAdded) {
            itemModel.status = StatusCheck;
        }else {
            itemModel.status = StatusPlusSign;
        }
    }
    cell.isEditing = _isEditing;
    cell.itemModel = group.items[indexPath.row];
    cell.indexPath = indexPath;
    return cell;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    
    if (kind == UICollectionElementKindSectionHeader) {
        ItemCellHeaderView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:sectionHeaderID forIndexPath:indexPath];
        
        ItemGroup *group = self.itemGroups[indexPath.section];
        headerView.title = group.type;
        
        return headerView;
    }else {
        return nil;
    }
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    if (section == 0) {
        return UIEdgeInsetsMake(0, 0, 10, 0);
    }else {
        return UIEdgeInsetsMake(0, 0, 1 / [UIScreen mainScreen].scale, 0);
    }
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
//    static BOOL hiden = NO;
//    hiden = !hiden;
//    [self setNavBarHidden:hiden];
    
    if (indexPath.section == 0) {
        NSLog(@"第一组 ");
        if (indexPath.row == 0) {
            MyCaseViewController *myCaseVC = [[MyCaseViewController alloc] init];
            [self.navigationController pushViewController:myCaseVC animated:YES];
        }
        if (indexPath.row == 1) {
            NewsViewController *newsVC = [[NewsViewController alloc] init];
            [self.navigationController pushViewController:newsVC animated:YES];
        }
        if (indexPath.row == 2) {
            ChargeViewController *chargeVC = [[ChargeViewController alloc] init];
            [self.navigationController pushViewController:chargeVC animated:YES];
        }
        if (indexPath.row == 3) {
            ChangeLawyerViewController *CLVC = [[ChangeLawyerViewController alloc] init];
            [self.navigationController pushViewController:CLVC animated:YES];
        }
        if (indexPath.row == 4) {
            FinanceViewController *financelVC = [[FinanceViewController alloc] init];
            [self.navigationController pushViewController:financelVC animated:YES];
        }
        if (indexPath.row == 5) {
            FinanceViewController *financelVC = [[FinanceViewController alloc] init];
            [self.navigationController pushViewController:financelVC animated:YES];
        }
        if (indexPath.row == 6) {
            ApprovalViewController *approvalVC = [[ApprovalViewController alloc] init];
            [self.navigationController pushViewController:approvalVC animated:YES];
        }
        if (indexPath.row == 7) {
            ApprovalViewController *approvalVC = [[ApprovalViewController alloc] init];
            [self.navigationController pushViewController:approvalVC animated:YES];
        }
    }
    if (indexPath.section == 1) {
        NSLog(@"第二组 ");
        if (indexPath.row == 0) {
            AddCaseViewController *addCaseVC = [[AddCaseViewController alloc] init];
            [self.navigationController pushViewController:addCaseVC animated:YES];
        }
        if (indexPath.row == 1) {
            MyCaseViewController *myCaseVC = [[MyCaseViewController alloc] init];
            myCaseVC.caseStyle = @"我的案件";
            myCaseVC.caseStateFlag = 0;
            myCaseVC.create_id = [InfoSingleTon personInfoSingleTon].infoDic[@"user"][@"id"];
            [self.navigationController pushViewController:myCaseVC animated:YES];
        }
        if (indexPath.row == 2) {
            MyCaseViewController *myCaseVC = [[MyCaseViewController alloc] init];
            myCaseVC.caseStyle = @"我的案件";
            myCaseVC.caseStateFlag = 1;
            myCaseVC.create_id = [InfoSingleTon personInfoSingleTon].infoDic[@"user"][@"id"];
            [self.navigationController pushViewController:myCaseVC animated:YES];
        }
        if (indexPath.row == 3) {
            MyCaseViewController *myCaseVC = [[MyCaseViewController alloc] init];
            myCaseVC.caseStyle = @"我的案件";
            myCaseVC.caseStateFlag = 2;
            myCaseVC.create_id = [InfoSingleTon personInfoSingleTon].infoDic[@"user"][@"id"];
            [self.navigationController pushViewController:myCaseVC animated:YES];
        }
        if (indexPath.row == 4) {
            MyCaseViewController *myCaseVC = [[MyCaseViewController alloc] init];
            myCaseVC.caseStyle = @"我的案件";
            myCaseVC.caseStateFlag = 3;
            myCaseVC.create_id = [InfoSingleTon personInfoSingleTon].infoDic[@"user"][@"id"];
            [self.navigationController pushViewController:myCaseVC animated:YES];
        }
        if (indexPath.row == 5) {
            ChangeLawyerViewController *CLVC = [[ChangeLawyerViewController alloc] init];
            [self.navigationController pushViewController:CLVC animated:YES];
        }
        if (indexPath.row == 6) {
            MyCaseViewController *myCaseVC = [[MyCaseViewController alloc] init];
            [self.navigationController pushViewController:myCaseVC animated:YES];
        }
        if (indexPath.row == 7) {
            CachetUseViewController *CUVC = [[CachetUseViewController alloc] init];
            [self.navigationController pushViewController:CUVC animated:YES];
        }
    }
    if (indexPath.section == 2) {
        NSLog(@"第三组 ");
        if (indexPath.row == 0) {
            ApprovalViewController *approvalVC = [[ApprovalViewController alloc] init];
            approvalVC.typeButtonIndex = 0;
            [self.navigationController pushViewController:approvalVC animated:YES];
        }
        if (indexPath.row == 1) {
            ApprovalViewController *approvalVC = [[ApprovalViewController alloc] init];
            approvalVC.typeButtonIndex = 1;
            [self.navigationController pushViewController:approvalVC animated:YES];
        }
        if (indexPath.row == 2) {
            NSLog(@"发票管理 ");
            InvoiceViewController *invoiceVC = [[InvoiceViewController alloc] init];
            [self.navigationController pushViewController:invoiceVC animated:YES];
        }
    }
    if (indexPath.section == 3) {
        NSLog(@"第四组 ");
        if (indexPath.row == 0) {
            FinanceViewController *financelVC = [[FinanceViewController alloc] init];
            financelVC.typeButtonIndex = 0;
            [self.navigationController pushViewController:financelVC animated:YES];
        }
        if (indexPath.row == 1) {
            FinanceViewController *financelVC = [[FinanceViewController alloc] init];
            financelVC.typeButtonIndex = 1;
            [self.navigationController pushViewController:financelVC animated:YES];
        }
        if (indexPath.row == 2) {
            NSLog(@"收费列表 ");
            ChargeViewController *chargeVC = [[ChargeViewController alloc] init];
            [self.navigationController pushViewController:chargeVC animated:YES];
        }
        if (indexPath.row == 3) {
            NSLog(@"律所收支 ");
            LawAccountViewController *LAVC = [[LawAccountViewController alloc] init];
            [self.navigationController pushViewController:LAVC animated:YES];
        }
    }
    if (indexPath.section == 4) {
        NSLog(@"第五组 ");
        if (indexPath.row == 0) {
            PunchCardViewController *PCVC = [[PunchCardViewController alloc] init];
            [self.navigationController pushViewController:PCVC animated:YES];
        }
        if (indexPath.row == 1) {
            NSLog(@"打卡记录 ");
            PunchRecordViewController *PRVC = [[PunchRecordViewController alloc] init];
            [self.navigationController pushViewController:PRVC animated:YES];
        }
        if (indexPath.row == 2) {
            NSLog(@"应用工具 ");
            ToolViewController *toolVC = [[ToolViewController alloc] init];
            [self.navigationController pushViewController:toolVC animated:YES];
        }
        if (indexPath.row == 3) {
            NSLog(@"公章审批 ");
            CachetApproveViewController *CAVC = [[CachetApproveViewController alloc] init];
            [self.navigationController pushViewController:CAVC animated:YES];
        }
    }
    if (indexPath.section == 5) {
        NSLog(@"第五组 ");
        if (indexPath.row == 0) {
            NSLog(@"律师助手 ");
            AssistantViewController *AVC = [[AssistantViewController alloc] init];
            [self.navigationController pushViewController:AVC animated:YES];
        }
    }
    
}
#pragma mark -- Getter/Setter


- (void)setTitle:(NSString *)title{
    [super setTitle:title];
    self.barTitleLable.text = title;
}
- (UIView *)barView{
    if (!_barView) {
        _barView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), 64)];
        _barView.backgroundColor = [UIColor colorWithRed:63.0f/255.0f green:189.0f/255.0f blue:240.0f/255.0f alpha:1.0f];
    }
    return _barView;
}
- (UILabel *)barTitleLable{
    if (!_barTitleLable) {
        _barTitleLable = [[UILabel alloc]init];
        _barTitleLable.bounds = CGRectMake(0, 0, 200, 20);
        _barTitleLable.center = CGPointMake(self.barView.center.x, self.barView.center.y + 10);
        _barTitleLable.text = @"办公";
        _barTitleLable.textAlignment = NSTextAlignmentCenter;
        _barTitleLable.font = [UIFont systemFontOfSize:18];
        _barTitleLable.textColor = [UIColor whiteColor];
        
    }
    return _barTitleLable;
}
- (NSArray *)itemGroups {
    if (!_itemGroups) {
        NSArray *datas = @[
                           @{@"type" : @"首页应用",@"items" :@[@{@"imageName" : @"list_05",@"itemTitle" : @"律所案件"},@{@"imageName" : @"list_06",@"itemTitle" :                                               @"新闻资讯"},@{@"imageName" : @"list_sf",@"itemTitle" : @"收费审批"},@{@"imageName" : @"list_09",@"itemTitle" : @"变更律师"},@{@"imageName" : @"list_28",@"itemTitle" : @"财务审核"},@{@"imageName" : @"list_24",@"itemTitle" : @"财务收案"},@{@"imageName" : @"list_25",@"itemTitle" : @"主任收案"},@{@"imageName" : @"list_26",@"itemTitle" : @"主任结案"}]},
                           @{@"type" : @"办公",@"items" : @[@{@"imageName" : @"list_07",@"itemTitle" : @"添加案件"},@{@"imageName" : @"list_ys",@"itemTitle" : @"预收案"},@{@"imageName" : @"list_sp",@"itemTitle" : @"审批中"},@{@"imageName" : @"list_ba",@"itemTitle" : @"办案中"},@{@"imageName" : @"list_ja",@"itemTitle" : @"已结案"},@{@"imageName" : @"list_09",@"itemTitle" : @"变更律师"},@{@"imageName" : @"list_05",@"itemTitle" : @"律所案件"},@{@"imageName" : @"list_sq",@"itemTitle" : @"公章使用"}]},
                           @{@"type" : @"审批",@"items" : @[@{@"imageName" : @"list_25",@"itemTitle" : @"收案审批"},@{@"imageName" : @"list_26",@"itemTitle" : @"结案审批"},
                               @{@"imageName" : @"list_fp",@"itemTitle" : @"发票管理"}
                                                   ]},
                           @{@"type" : @"财务统计 ",@"items" : @[@{@"imageName" : @"list_28",@"itemTitle" : @"财务收案"},@{@"imageName" : @"list_24",@"itemTitle" : @"财务结案"},@{@"imageName" : @"list_sf",@"itemTitle" : @"收费审批"},@{@"imageName" : @"list_23",@"itemTitle" : @"律所收支"}]},
                           @{@"type" : @"行政",@"items" : @[@{@"imageName" : @"list_dk",@"itemTitle" : @"签到打卡"},@{@"imageName" : @"list_jl",@"itemTitle" : @"打卡记录"},@{@"imageName" : @"gj08",@"itemTitle" : @"应用工具"},@{@"imageName" : @"list_27",@"itemTitle" : @"公章审批"}]},
                           @{@"type" : @"律师助手",@"items" : @[@{@"imageName" : @"list_10",@"itemTitle" : @"律师助手"}]}];
        
        
        NSMutableArray *array = [NSMutableArray arrayWithCapacity:datas.count];
        NSMutableArray *allItemModels = [[NSMutableArray alloc] init];
        for (NSDictionary *dict in datas) {
            ItemGroup *group = [[ItemGroup alloc] initWithDict:dict];
            if ([group.type isEqualToString:@"首页应用"]) {
                for (ItemModel *model in group.items) {
                    model.status = StatusMinusSign;
                }
            }else {
                [allItemModels addObjectsFromArray:group.items];
            }
            [array addObject:group];
        }
        _itemGroups = [array copy];
        _allItemModel = [allItemModels copy];
    }
    
    return _itemGroups;
}
@end
